package com.weme.common.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.weme.common.util.loadImage
import com.weme.common.R
import dev.kratos.api.user_server.v1.GiftInfo

class BannerAdapter(var context : Context,private val dataList: List<List<GiftInfo>>,   val callback: (GiftInfo) -> Unit) :
    RecyclerView.Adapter<BannerAdapter.BannerViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_banner_item, parent, false)
        return BannerViewHolder(view)
    }

    override fun onBindViewHolder(holder: BannerViewHolder, position: Int) {
        val rowData = dataList[position]
        Log.e("tag",position.toString())
        Log.e("tag",holder.absoluteAdapterPosition.toString())
        holder.rcvList.layoutManager = GridLayoutManager(context, 4)
        holder.rcvList.adapter = giftChindAdapter
        giftChindAdapter.submitList(rowData)
        giftChindAdapter.setOnItemClickListener { _, _, index ->
            callback.invoke(rowData[index])

        }
      //  Log.e("tag",rowData.size.toString())
    }


    private val giftChindAdapter = object : BaseQuickAdapter<GiftInfo, QuickViewHolder>() {
        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: GiftInfo?) {
            item?.let {
                val imGift = holder.getView<ImageView>(R.id.imGift)
                imGift.loadImage(it.giftIcon)

                holder.setText(R.id.tvGift,it.giftPrice.toString())
            }
        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            return QuickViewHolder(R.layout.item_gift_chind, parent)
        }


    }
    override fun getItemCount(): Int {
        return dataList.size
    }

    inner class BannerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        // 在这里声明ViewHolder中的控件
        var rcvList: RecyclerView = itemView.findViewById(R.id.rcvList)
    }
}